/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.Enum.CardStatusEnum;
import com.ykht.proj.activity.Enum.PayStatusEnum;
import com.ykht.proj.activity.constant.CardConstant;
import com.ykht.proj.activity.entity.*;
import com.ykht.proj.activity.mapper.VOrderCardMapper;
import com.ykht.proj.activity.service.VCardService;
import com.ykht.proj.activity.service.VDiscountPlanService;
import com.ykht.proj.activity.service.VOrderCardService;
import com.ykht.proj.activity.vo.*;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.CnLabel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单和卡的关联表
 *
 * @author csg
 * @date 2021-05-21 14:22:31
 */
@Slf4j
@Service
public class VOrderCardServiceImpl extends ServiceImpl<VOrderCardMapper, VOrderCard> implements VOrderCardService {

	@Resource
	private VOrderCardMapper vOrderCardMapper;
	@Autowired
	private VCardService vCardService;
	@Autowired
	private VDiscountPlanService vDiscountPlanService;

	/**
	 * 新增/修改 卡激活有效期
	 *
	 * @param vOrderCard
	 */
	@Override
	public void updateActivationTime(VOrderCard vOrderCard) {
		UpdateWrapper<VOrderCard> updateWrapper = new UpdateWrapper();
		updateWrapper.lambda().eq(VOrderCard::getOrderId, vOrderCard.getOrderId())
				.eq(VOrderCard::getCardId, vOrderCard.getCardId())
				.set(VOrderCard::getStartTime, vOrderCard.getStartTime())
				.set(VOrderCard::getEndTime, vOrderCard.getEndTime())
				.set(VOrderCard::getUpdateTime, LocalDateTime.now())
				.set(VOrderCard::getUpdateUser, SecurityUtils.getUserId());
		this.update(updateWrapper);
	}

	/**
	 * 企业购买卡列表 分页查询
	 *
	 * @param page         分页对象
	 * @param vOrderCardVo 订单和卡的关联表
	 * @return
	 */
	@Override
	public IPage<VOrderCardVo> getEntOrderCardPage(Page page, VOrderCardVo vOrderCardVo) {
		return vOrderCardMapper.getEntOrderCardPage(page, vOrderCardVo);
	}

	/**
	 * 企业购买卡详情列表 分页查询
	 *
	 * @param page         分页对象
	 * @param vOrderCardVo 订单和卡的关联表
	 * @return
	 */
	@Override
	public IPage<VOrderCardVo> getEntOrderCardDetailsPage(Page page, VOrderCardVo vOrderCardVo) {
		return vOrderCardMapper.getEntOrderCardDetailsPage(page, vOrderCardVo);
	}

	@Override
	public List<OrderGroup> orderGroup(Long orderId) {
		return vOrderCardMapper.getOrderGroup(orderId);
	}

	/**
	 * 企业 导出兑换码excel
	 *
	 * @param vOrderCardVo
	 * @return
	 */
	@Override
	public List<VOrderCardVo> getEntExportRedeemCode(VOrderCardVo vOrderCardVo) {
		return vOrderCardMapper.getEntExportRedeemCode(vOrderCardVo);
	}

	@Override
	public R<OrderDetail> getOrderDetail(Long orderId) {
		OrderDetail detail = new OrderDetail();
		VOrderCard orderCard = this.getOne(Wrappers.lambdaQuery(VOrderCard.class).eq(VOrderCard::getOrderId,orderId));
		VCard card = new VCard();
		List<CnLabel> labels = new ArrayList<>();
		if (orderCard!=null) {
			card = card.selectById(orderCard.getCardId());
			labels = vCardService.cardLabel(card.getId());
		}

		BigDecimal discount = vDiscountPlanService.minDiscount(card,orderCard);
		//折扣
		detail.setDiscount(discount);
		detail.setCard(card);
		detail.setCardLabel(labels.stream().map(CnLabel::getName).collect(Collectors.toList()));
		detail.setOrderCard(orderCard);
		return R.ok(detail);
	}

}
